package com.coincalf.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.admin.entity.SysMenu;
import com.coincalf.admin.entity.SysPrivilege;
import com.coincalf.admin.entity.SysUser;
import com.coincalf.admin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qiang
 */
@Service
public class SysUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;
    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        ew.eq("username", username);
        SysUser user = sysUserService.selectOne(ew);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        }

        //查询菜单信息并归集
        //List<SysMenu> menus = sysMenuService.childs(sysMenuService.selectListByUserId(user.getId()));

        //查询权限信息
        List<SysPrivilege> privileges;
        List<SysMenu> menus;
        //如果是管理员，加载所有权限和菜单
        if (sysUserRoleService.isAdminUser(user.getId())) {
            privileges = sysPrivilegeService.selectList(new EntityWrapper<SysPrivilege>());
            menus = sysMenuService.selectList(new EntityWrapper<SysMenu>());
        } else {
            privileges = sysPrivilegeService.selectListByUserId(user.getId());
            menus = sysMenuService.selectListByUserId(user.getId());
            List<String> ids = new ArrayList<>();
            menus.forEach(m -> {
                ids.add(m.getParentKey());
            });
            EntityWrapper<SysMenu> sysMenuEntityWrapper = new EntityWrapper<>();
            sysMenuEntityWrapper.in("id", ids);
            List<SysMenu> parentSysMenuList = sysMenuService.selectList(sysMenuEntityWrapper);
            menus.addAll(parentSysMenuList);
        }
/*        List<String> strings = sysMenuService.strings(menus);
        user.setMenuStrings(strings);*/

        user.setMenus(menus);
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        for (SysPrivilege privilege : privileges) {
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(privilege.getName());
            grantedAuthorities.add(grantedAuthority);
        }

        user.setAuthorities(grantedAuthorities);
        return user;
    }

}
